﻿#include "PixelFormatConversionToolWindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <thread>


PixelFormatConversionToolWindow::PixelFormatConversionToolWindow(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	this->setWindowTitle("像素格式转换工具");
	setAttribute(Qt::WA_DeleteOnClose, true);  // 关闭时自动销毁窗口
	tool = PixelFormatConversionTool::Create();

}

void PixelFormatConversionToolWindow::startChange()
{
	srcWidth = ui.lineEdit_src_width->text().toInt();
	srcHeight = ui.lineEdit_src_height->text().toInt();
	targetWidth = ui.lineEdit_target_width->text().toInt();
	targetHeight = ui.lineEdit_target_height->text().toInt();
	inPixFormat = PixelFormatConversionTool::YUV420P;
	outPixFormat = PixelFormatConversionTool::RGBA;
	if (this->srcWidth <= 0 || this->srcHeight <= 0)
	{
		QMessageBox::warning(nullptr, "警告", "源文件的宽高必须大于0");
		return;
	}
	else if (this->targetWidth <= 0 || this->targetHeight <= 0)
	{
		QMessageBox::warning(nullptr, "警告", "目标文件的宽高必须大于0");
		return;
	}
	else if (this->inFilePath.isEmpty())
	{
		QMessageBox::warning(nullptr, "警告", "请选择源文件");
		return;
	}
	else if (this->outFilePath.isEmpty())
	{
		QMessageBox::warning(nullptr, "警告", "请选择目标文件的存储位置");
		return;
	}

	tool->Init(
		this->srcWidth,
		this->srcHeight,
		this->targetWidth,
		this->targetHeight,
		this->inFilePath.toStdString(),
		this->outFilePath.toStdString(),
		this->inPixFormat,
		this->outPixFormat
	);
	//开启线程单独跑
	std::thread t([this]() {
		tool->Start();
		});
	t.detach();
}

void PixelFormatConversionToolWindow::choiceSrcFile()
{
	QString filter = "RGB 文件 (*.*);;所有文件 (*.*)";
	// 打开文件选择对话框，选择单个文件
	QString filePath = QFileDialog::getOpenFileName(this, "选择 RGB 文件", "", filter);
	this->inFilePath = filePath;
	this->ui.lineEdit_src_path->setText(this->inFilePath);
}

void PixelFormatConversionToolWindow::choiceTargetFilePath()
{
	QString filter = "所有文件(*.*)"; //文件过滤器
	QString filePath = QFileDialog::getSaveFileName(this, "选择文件输出目录", "", filter);
	this->outFilePath = filePath;
	this->ui.lineEdit_target_path->setText(this->outFilePath);

}

void PixelFormatConversionToolWindow::onSrcPixformatChanged(int pos)
{
	switch (pos)
	{
	case 0:
		inPixFormat = PixelFormatConversionTool::RGBA;
		break;
	case 1:
		inPixFormat = PixelFormatConversionTool::ARGB;
		break;
	case 2:
		inPixFormat = PixelFormatConversionTool::RGB24;
		break;
	case 3:
		inPixFormat = PixelFormatConversionTool::YUV420P;
		break;
	}
}

void PixelFormatConversionToolWindow::choiceTargetPixformatFilePath(int pos)
{
	switch (pos)
	{
	case 0:
		outPixFormat = PixelFormatConversionTool::YUV420P;
		break;
	case 1:
		outPixFormat = PixelFormatConversionTool::RGB24;
		break;
	case 2:
		outPixFormat = PixelFormatConversionTool::ARGB;
		break;
	case 3:
		outPixFormat = PixelFormatConversionTool::RGBA;
		break;
	}
}

void PixelFormatConversionToolWindow::closeEvent(QCloseEvent* event) {
	event->accept();  // 接受关闭事件（允许关闭）
	qDebug() << "窗口已关闭";
	if (tool) {
		tool->Close();
	}
}

void PixelFormatConversionToolWindow::resizeEvent(QResizeEvent* event) {
	// 调用父类实现（保留默认行为）
	QWidget::resizeEvent(event);
}

PixelFormatConversionToolWindow::~PixelFormatConversionToolWindow()
{
}
